package voice;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import com.dudu.bean.DialResultBean;
import com.dudu.bean.ResponseSendVoiceBean;
import com.dudu.service.ILogService;
import com.dudu.service.impl.LogService;
import com.dudu.utils.DateUtils;
import com.dudu.utils.DictionaryUtils;
import com.dudu.utils.Encrypt;
import com.dudu.utils.HttpRequest;

import core.CoreService;

/**
 * 自建通道
 * 
 * @author qinfx
 * 
 */
public class SelfSupportUtil {

    private static String ServerUrl = "http://172.20.8.52:8080/";//DictionaryUtils.getValueByTypeAndKey("selfSupport", "url");
    private static String secret = "cass@dudu"; //
    private static ILogService logService = new LogService();

    public static void main(String[] agr) throws Exception {
    	//SelfSupportUtil.UploadFile(null, null, true);
    	SelfSupportUtil.VoiceNotice("", null, null, null, null, null, true);
    }

    /**
     * 语音通知
     * 
     * @param called
     *            被叫号码
     * @param mediaName
     *            语音文件名称，播放多个文件用英文逗号隔开。与mediaTxt不能同时为空。当不为空时mediaTxt属性失效
     * @param mediaTxt
     *            文本内容，文本中汉字要求utf8编码，默认值为空。当mediaName为空才有效
     * @param displayNum
     *            来电显示的号码，根据平台侧显号规则控制
     * @param respUrl
     *            外呼通知状态通知回调地址
     * @param userData
     * @param 是否青岛项目请求
     *            ，青岛项目适用单独的token
     * @return
     */
    public static ResponseSendVoiceBean VoiceNotice(String called, String mediaName, String mediaTxt, String displayNum, String respUrl, String userData, boolean isQingDao) {
	ResponseSendVoiceBean responseSendVoice = new ResponseSendVoiceBean();
	long begin = Calendar.getInstance().getTimeInMillis();
	JSONObject object = new JSONObject();
	JSONArray requestArray = new JSONArray();
	String responsedata = "";
	String result = "";
	String sessionid = "";
	String desc="";
	String sign = "";
	String timestmap = String.valueOf(System.currentTimeMillis());
	try {
		JSONArray voiceArray = new JSONArray();
	    JSONObject voiceText = new JSONObject(); //文字正文
	    voiceText.put("voiceType", "text");
	    voiceText.put("content", "hello, TTS!");
	    voiceText.put("index", "0");
	    JSONObject voiceMedia = new JSONObject(); //语音文件
	    voiceMedia.put("voiceType", "file");
	    voiceMedia.put("content", "music.wav");
	    voiceMedia.put("index", "0");
	    //voiceArray.add(voiceText);
	    voiceArray.add(voiceMedia);
	    JSONObject voiceTts = new JSONObject(); //前导音
	    voiceTts.put("voiceType", "text");
	    voiceTts.put("content", "用友移动通信欢迎您");
	    voiceTts.put("index", "0");
		
		
	    JSONObject announcement = new JSONObject();
	    announcement.put("serial", UUID.randomUUID().toString());
	    announcement.put("caller", "62434318"); //显号
	    announcement.put("called", "18662187762"); 
	    announcement.put("announcements", voiceArray);
	    announcement.put("repeat", "3");
	    announcement.put("callEventUrl", "");
	    announcement.put("callRecordUrl", "");
	    announcement.put("repeatType", "2");
	    announcement.put("priority", "1");
	    announcement.put("leadingVoice", "");
	    announcement.put("ttsParams", "woman");
	    requestArray.add(announcement);
	    
		object.put("sign", Encrypt.md5Encrypt("time="+timestmap+"&secret="+secret).toUpperCase()); //签名
	    object.put("time", timestmap);// 发送时间
	    object.put("requestCount", 1);// 呼叫个数
	    object.put("requests", requestArray);// 呼叫任务
	    
	    String Myurl = ServerUrl + "ivr/v1/announce";
	    responsedata = HttpRequest.postData(Myurl, object.toString());
	    System.out.println(responsedata);
	    //CoreService.log.info("VoiceNotice req:"+object.toString()+"response:"+responsedata);
	    /*if (StringUtils.isNotBlank(responsedata)) {
		JSONObject response = JSONObject.fromObject(responsedata);
		// 前导音为文本是使用%C 前导音为语音文件时使用%V吗？
		result = response.optString("statusCode");
		if (result.equals("00000")) {
		    result = "0";
		    sessionid = response.optString("callSid");
		    desc="OK";
		}else{
		   desc=response.optString("ExceptionMessage") ;
		}
	    } else {
		responsedata = "";
		desc="接口未响应";
	    }*/

	} catch (Exception e) {
	    e.printStackTrace();
	    return null;
	}
	/*long end = Calendar.getInstance().getTimeInMillis();
	double time = Double.valueOf((end - begin)) / 1000;
	logService.logIms(time, "TianYiNotice", "【" + object.toString() + "】", "【" + responsedata + "】", result, DateUtils.getCurrentTime(), "TianYi", called);
	responseSendVoice.setResult(result);
	responseSendVoice.setSessionid(sessionid);
	responseSendVoice.setDesc(desc);*/
	return responseSendVoice;
    }

    /**
     * 上传文件 1、先把mp3格式转成wav 2、把文件转成二进制流
     * 
     * @param fileName
     * @param completeFileName
     * @return
     */
    public static ResponseSendVoiceBean UploadFile(String fileName, String completeFileName, boolean isQingDao) {
	ResponseSendVoiceBean bean = new ResponseSendVoiceBean();
	// 记录发起的开始时间
	Long begin = Calendar.getInstance().getTimeInMillis();
	String timestmap = String.valueOf(System.currentTimeMillis());
	try {
	    // 判断是否为mp3文件
	    
	    String url = ServerUrl + "file/v1/voiceUpload";
	    JSONObject params = new JSONObject();
	    params.put("sign", Encrypt.md5Encrypt("time="+timestmap+"&secret="+secret).toUpperCase());
	    params.put("time", timestmap);
	    params.put("voiceFile", "duduopen/201702192338032303.mp3");
	    String req = url + "?" + params.toString();
	    
	    String responsedata =HttpRequest.postData(url, params.toString());
	    System.out.println("UploadFile req:"+req+"---response:"+responsedata);
	   /* CoreService.log.info("UploadFile req:"+req+"---response:"+responsedata);
	    String result = "-1";
	    if (StringUtils.isNotBlank(responsedata)) {
		JSONObject response = JSONObject.fromObject(responsedata);
		result = response.optString("statusCode");
		if (StringUtils.isNotBlank(result) && result.equals("00000")) {
		    result = "0";
		} else {
		    result = "-1";
		}
	    } else {
		responsedata = "";
	    }

	    Long end = Calendar.getInstance().getTimeInMillis();
	    Double during = Double.valueOf((end - begin)) / 1000;
	    logService.logIms(during, "UploadFile", "上传文件-tianyi-" + fileName, "【" + responsedata + "】", result + "", DateUtils.getCurrentTime(), "TianYi", "");
	    bean.setResult(result);
	    bean.setFileName("");*/
	    return bean;
	} catch (Exception e) {
	    e.printStackTrace();
	    CoreService.log.error("文件："+fileName+"上传失败");
	}
	bean.setResult("999999");
	bean.setFileName("");
	return bean;
    }

}
